package cn.com.jslh.boarpro.util.timedialog;

import android.app.DatePickerDialog;
import android.content.Context;
import android.widget.DatePicker;
import android.widget.TextView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 作者：周洲 on 2017/3/22 14:37
 * <p>
 * 邮箱：double0zhou@126.com
 */

public class DateChoseDialogUtil{
    public static void showTimeDialog(Context context, final DatePickerListener listener){
        Calendar now = Calendar.getInstance();
        DatePickerDialog datePickerDialog = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                String dayStr =dayOfMonth+"";
                if(dayOfMonth<10)
                    dayStr ="0"+dayStr;
                if(month<9)
                    listener.picker(year + "-0" + (month + 1) + "-" + dayStr);
                else
                    listener.picker(year + "-" + (month + 1) + "-" + dayStr);
            }
        }, now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH));
//        datePickerDialog.getDatePicker().setMaxDate(new Date().getTime());
        datePickerDialog.setCancelable(false);
        datePickerDialog.show();
    }

    /**
     *
     * @param context
     * @param min 最小时间
     * @param max 最大时间
     * @param listener
     */
    public static void showTimeDialog(Context context,int min,int max, final DatePickerListener listener){
        Calendar now = Calendar.getInstance();
        DatePickerDialog datePickerDialog = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                String dayStr =dayOfMonth+"";
                if(dayOfMonth<10)
                    dayStr ="0"+dayStr;
                if(month<9)
                    listener.picker(year + "-0" + (month + 1) + "-" + dayStr);
                else
                    listener.picker(year + "-" + (month + 1) + "-" + dayStr);
            }
        }, now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH));
        Date today  = new Date();
        Calendar cal=Calendar.getInstance();
        cal.setTime(today);
        cal.add(Calendar.DATE, min);  //
        datePickerDialog.getDatePicker().setMinDate(cal.getTime().getTime());
        cal.setTime(today);
        cal.add(Calendar.DATE, max);  //
        datePickerDialog.getDatePicker().setMaxDate(cal.getTime().getTime());
        datePickerDialog.setCancelable(false);
        datePickerDialog.show();
    }

    public static void showTimeDialog(final TextView editText){
        showTimeDialog(editText.getContext(), new DatePickerListener() {
            @Override
            public void picker(String date) {
                editText.setText(date);
            }
        });
    }

    public static void showTimeDialogs(TextView... textViews){
        for (TextView textview: textViews) {
            showTimeDialog(textview);
        }
    }
}
